const express=require('express');

//引入pool模块
const pool=require('./pool.js');

//创建路由器
const userRouter=express.Router();





//注册接口 增加用户
userRouter.post('/reg',(req,res,next)=>{

    var obj=req.body;
    
	//手机格式验证 
	if(!/^1[3-9]\d{9}$/.test(obj.phone)){
		res.send({code:405,msg:'手机格式错误'});
		console.log(obj.phone);
        console.log(obj);
		return;
	}
	
  
    /*
	//邮箱格式验证
    if(!/^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/.test(obj.phone)){
		res.send({code:405,msg:'邮箱格式错误'});
		return;
	}
	*/
   


    //用户名不能重复
    pool.query('select * from xz_user',(err,re)=>{
		
         if(err){
			next(err);
         }


         //console.log(re);

		 //console.log(re[0]['uname']);

         
         for(var i=0;i<re.length;i++){
			
			  console.log(re[i]['uname']);
			
			
			//用户名不能重复
			if(obj.uname===re[i]['uname']){
				res.send({code:500,msg:'用户名已被注册'});
				return;
			}
     
	      
	        //手机号不能重复   
			if(obj.phone===re[i]['phone']){
				res.send({code:500,msg:'手机号已被使用'});
				return;
			}
			

         }
         


    
        //插入注册数据 
        pool.query('insert into xz_user set?',[obj],(err,re)=>{
		
			if(err){
				next(err);
				return;
			}
		

			if(re.affectedRows){
				res.send({code:200,msg:'注册成功'});
			}

			else{
				res.send({code:500,msg:'注册失败'});
			}

       });


    });
  
});






//删除用户接口
userRouter.delete('/delete/:id',(req,res,next)=>{
	
      var id=req.params.id;
      //console.log(id);


      //执行删除
      pool.query('delete from xz_user where uid=?',[id],(err,re)=>{
		
		    if(err){
				next(err);
				return;
		    }

            if(re.affectedRows){
				res.send({code:200,msg:'删除用户成功'});
            }

			else{
				res.send({code:500,msg:'删除用户失败'});
			}

      });

});








//修改用户信息
userRouter.put('/',(req,res,next)=>{
	
	 var modifyObj=req.body;

	 pool.query('update xz_user set? where uid=?',[modifyObj,modifyObj.uid],(err,re)=>{
		    
			if(err){
				next(err);
				return;
			}

			
			//console.log(re);

			if(re.affectedRows){
				res.send({code:200,msg:'修改成功'});
			}
			
			else{
				res.send({code:500,msg:'修改失败'});
			}

	 });

});










//查询接口
userRouter.get('/select/:id',(req,res,next)=>{

    var uid=req.query.id;

    pool.query('select * from xz_user where uid=',[uid],(err,re)=>{
		if(err){
			next(err);
			return;
		}

        if(re.length){
			res.send({code:200,msg:'注册成功'});
        }

		else{
			res.send({code:500,msg:'注册失败'});
		}

    });


	
});








//登录接口
userRouter.post('/login',(req,res,next)=>{
	
	var obj=req.body;


    if(!obj.uname){
		 res.send({code:401,msg:'用户名不能为空'});
    }

	if(!obj.upwd){
		 res.send({code:401,msg:'密码不能为空'});
    }

    //console.log(obj);

    
	pool.query('select * from xz_user where uname=? and upwd=?',[obj.uname,obj.upwd],(err,re)=>{
		
          if(err){
			 next(err);
			 return;
          }

          //console.log(re);
            
          
		  if(re.length){
			 res.send({code:200,msg:'登录成功'});
		  }

		  else{
			 res.send({code:500,msg:'登录失败'});
		  }
          

	});
	

});








module.exports=userRouter;